package com.frank.controller;

import com.frank.dto.JsonResult;
import com.frank.dto.LoginDto;
import com.frank.service.StaffService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/auth/staff")
public class StaffController {

    @Resource
    private StaffService staffService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JsonResult<?> login(@RequestParam("phoneNum") String phoneNum, @RequestParam("password") String password) {
        LoginDto loginDto = staffService.loginService(phoneNum, password);
        if (loginDto != null){
            return new JsonResult<>(200, "登录成功", loginDto);
        }else {
            return new JsonResult<>(404, "手机号码错误或密码错误");
        }

    }

    @Transactional
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public JsonResult<?> register(@RequestParam("phoneNum") String phoneNum, @RequestParam("password") String password) {
        if (staffService.registerService(phoneNum, password)){
            return new JsonResult<>(200, "注册成功");
        }else {
            return new JsonResult<>(500, "注册失败");
        }
    }

    @RequestMapping(value = "/checkToken", method = RequestMethod.GET)
    public JsonResult<?> checkToken(@RequestParam("token") String token) {
        if (staffService.checkToken(token)){
            return new JsonResult<>(200, "有效token");
        }else {
            return new JsonResult<>(401, "无效token");
        }
    }
}
